package main

import (
	"11-middleware/middleware"
	"log"
	"math/rand"
	"net/http"
	"time"
)

// logging 安全 请求超时 响应压缩
// 黑白名单
// 请求上下文
// https ssl 安全套接层 tsl 传输层安全
// 生成证书
// go run /usr/local/go/src/crypto/tls/generate_cert.go -host localhost

// -- 部署 --
// 后台启动运行
// 1. nohup ./server &
// 2. systemd 作为系统服务运行
// 3. 容器运行
func main() {

	http.HandleFunc("/", func(writer http.ResponseWriter,
		request *http.Request) {
		rand.Seed(time.Now().Unix())
		// 添加随机等待时间, 模拟超时
		time.Sleep(time.Second * time.Duration(rand.Intn(5)))
		// time.Sleep(time.Second * 4)
		writer.Write([]byte("success!"))
	})

	log.Fatalln(http.ListenAndServeTLS("localhost:8080",
		"cert.pem", "key.pem",
		&middleware.TimeOut{
			Next: &middleware.Auth{
				Next: http.DefaultServeMux,
			},
		},
	))
}
